//
//  TitleTableView.m
//  WuliHanhan
//
//  Created by tang on 16/3/17.
//  Copyright © 2016年 LVpeng. All rights reserved.
//

#import "TitleTableView.h"
#import "Header.h"


#import "TitleShowImageTableViewCell.h"

@interface TitleTableView ()<UITableViewDataSource, UITableViewDelegate>
{
    UITableView *mainTableView;
    NSMutableArray *mutableArray;
    NSMutableArray *selectArray;
    NSMutableArray *nameMutableArray;
    
    NSIndexPath *SELECTINT;
}

@end



@implementation TitleTableView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIImage *image = nil;
        
        image = [UIImage imageNamed:@"fanhui.png"];
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 60)];
        [self addSubview:backButton];
        [backButton setImage:image forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(backToPop) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        UIButton *nextButton = [[UIButton alloc] initWithFrame:CGRectMake(WithView-57.0, 0, 57.0, 60.0)];
        [nextButton setTitle:@"完成" forState:UIControlStateNormal];
        [self addSubview:nextButton];
        [nextButton.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        [nextButton setTitleColor:uicolor(254, 195, 9) forState:UIControlStateNormal];
        [nextButton addTarget:self action:@selector(imageFinished) forControlEvents:UIControlEventTouchUpInside];
        
        
        selectArray = [NSMutableArray new];
        
        [self initUI];
    }
    return self;
}




- (void)initUI
{
    mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 60, WithView-87)];
    [mainTableView setShowsVerticalScrollIndicator:YES];
    [mainTableView setShowsHorizontalScrollIndicator:YES];
    [mainTableView setBackgroundColor:[UIColor clearColor]];
    //    [mainTableView setBounces:NO];
    [mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self addSubview:mainTableView];
    [mainTableView setCenter:CGPointMake((WithView-87.0)/2.0+30, 30)];
    [mainTableView setTransform:CGAffineTransformMakeRotation(-M_PI/2.0)];
    [mainTableView setDataSource:self];
    [mainTableView setDelegate:self];
}



- (void)setSelectInt:(int)selectInt
{
    //    NSLog(@"%d", selectInt);
    
    SELECTINT = [NSIndexPath indexPathForRow:selectInt inSection:0];
    
    [mainTableView selectRowAtIndexPath:SELECTINT animated:YES scrollPosition:UITableViewScrollPositionBottom];
}


//- (void)setDataArray:(NSArray *)dataArray
//{
//    mutableArray = [[NSMutableArray alloc] init];
//    for (NSData *data in dataArray)
//    {
//        UIImage *image = [UIImage imageWithData:data];
//        [mutableArray addObject: UIImageJPEGRepresentation(image, 0.1)];
//    }
//    [mainTableView reloadData];
//}



- (void)setDataArray:(NSArray *)dataArray
{
//    NSLog(@"%@", dataArray);
    mutableArray = [[NSMutableArray alloc] init];
    [mutableArray addObjectsFromArray:dataArray];
    [mainTableView reloadData];
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 47.0;
}



//重载
- (void)tableReload
{
    [mainTableView reloadData];
}



//返回
- (void)backToPop
{
    if (_MTitleTableViewDelegate && [_MTitleTableViewDelegate respondsToSelector:@selector(popToView)])
    {
        [_MTitleTableViewDelegate popToView];
    }
}

//完成
- (void)imageFinished
{
    if (_MTitleTableViewDelegate && [_MTitleTableViewDelegate respondsToSelector:@selector(wanchengBuoont)]) {
        [_MTitleTableViewDelegate wanchengBuoont];
    }
}


#pragma mark ---- datasource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([mutableArray count] == 9)
    {
        return 9;
    }
    else
    {
        return [mutableArray count]+1;
    }
    
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInden = @"cell";
    TitleShowImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInden];
    if (!cell) {
        cell = [[TitleShowImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInden];
    }
    if (indexPath.row < [mutableArray count])
    {
        
        [cell setImageModelWithDataBase:[UIImage imageWithData:[mutableArray objectAtIndex:indexPath.row]]];
    }
    else
    {
        [cell setImageModelWithDataBase:[UIImage imageNamed:@"tianjia.png"]];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    [cell setTransform:CGAffineTransformMakeRotation(M_PI/2.0)];
    return cell;
}




#pragma mark----dalegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //点击增加图片
    if (indexPath.row == [mutableArray count])
    {
        if (_MTitleTableViewDelegate && [_MTitleTableViewDelegate respondsToSelector:@selector(tianjiaImage)]) {
            [_MTitleTableViewDelegate tianjiaImage];
        }
    }
    else    //改变现实图片
    {
        if (SELECTINT.row == indexPath.row)
        {
        }
        else
        {
            
            [_MTitleTableViewDelegate chooseCurrentWithName:[NSString stringWithFormat:@"%d", (int)indexPath.row] lastWithName:[NSString stringWithFormat:@"%d", (int)SELECTINT.row]];
            
            TitleShowImageTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            [cell tapSelectImage];
            TitleShowImageTableViewCell *cell2 = [tableView cellForRowAtIndexPath:SELECTINT];
            [cell2 removeTapSelectImage];
            SELECTINT = [indexPath copy];
            
            
            
        }
        
        //        [_MTitleTableViewDelegate chooseImageWithTitleView:indexPath.row nameIndex:[nameMutableArray objectAtIndex:indexPath.row]];
    }
}






/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
